/*
 * LcdWidget.h
 *
 *  Created on: 2012-11-27
 *      Author: kylin
 */

#ifndef LCDWIDGET_H_
#define LCDWIDGET_H_

#include <QList>
#include <QMap>
#include <QString>
#include <QObject>
#include <QVariant>
#include <QStringList>

#include "gtype.h"

class QTimer;

class DprLockManager;
class DprLcd;

enum KeyAction
{
	kaNone=0,
	kaActivateSub=1,
};

typedef struct _tKeyEvent
{
	unsigned int keyCode;
	KeyAction action;
	QVariant actionData;
}KeyEvent;

class LcdWidget : public QObject
{
public:
	LcdWidget(int id,const QString &title,DprLockManager* lockManager,
			DprLcd *lcd,LcdWidget *parent=0);
	virtual ~LcdWidget();

public:
	int id(){return m_id;}
	QString title(){return m_title;}
	LcdWidget* parent(){return m_parent;}

	// Put the widget to its initial state,the widget will not draw
	// itself until show() is called.When leaving this widget,call this
	// function to stop any automatic drawing that was started before.
	virtual void reset()=0;

	// Process the key event associated with this widget.
	virtual void keyEvent(KeyEvent &event)=0;

	// Refresh the widget's internal state.
	virtual void update()=0;

	// When this function is called,the widget starts drawing itself.
	virtual void show()=0;

	// Set the title of the widget
	void setTitle(const QString & title){m_title=title;}

protected:
	virtual void draw()=0;

protected:
	int m_id;
	QString m_title;
	DprLockManager *m_lockManager;
	DprLcd *m_lcd;
	LcdWidget *m_parent;
};

class LcdWidgetMain : public LcdWidget
{
	Q_OBJECT
public:
	LcdWidgetMain(int id,const QString &title,DprLockManager* lockManager,
			DprLcd *lcd,LcdWidget *parent=0);
	virtual ~LcdWidgetMain();

public:
	virtual void reset();
	virtual void keyEvent(KeyEvent &event);
	virtual void update();
	virtual void show();

protected:
	virtual void draw();

	virtual void timerEvent(QTimerEvent *event);

private:
	int m_timerId;
};

class LcdWidgetMenu : public LcdWidget
{
public:
	LcdWidgetMenu(int id,const QString &title,DprLockManager* lockManager,
			DprLcd *lcd,LcdWidget *parent=0);
	virtual ~LcdWidgetMenu();

public:
	virtual void reset();
	virtual void keyEvent(KeyEvent &event);
	virtual void update();
	virtual void show();

	void setMenuList(const QStringList &lstMenu){m_lstMenuText=lstMenu;}

protected:
	virtual void draw();

private:
	void drawMenuItem(int index);

private:
	QStringList m_lstMenuText;
	int m_curMenu;
};

class LcdWidgetEvent : public LcdWidget
{
public:
	LcdWidgetEvent(int id,const QString &title,DprLockManager* lockManager,
			DprLcd *lcd,LcdWidget *parent=0);
	virtual ~LcdWidgetEvent();

public:
	virtual void reset();
	virtual void keyEvent(KeyEvent &event);
	virtual void update();
	virtual void show();

	void setSoeEvent(DprSoeEvent &event);
	void setAlarmEvent(DprAlarmEvent &event);

protected:
	virtual void draw();
};

enum ItemEditDelegateType
{
	iedtReadOnly=0,
	iedtInteger=1,
	iedtCombo=2,
};

typedef struct _tLWTItemEditDelegate
{
	ItemEditDelegateType type;
	QVariant data;
}LWTItemEditDelegate;

typedef struct _tLcdWidgetTableItem
{
	QString name;
	QString value;
	LWTItemEditDelegate* delegate;
}LcdWidgetTableItem;

class LcdWidgetTable : public LcdWidget
{
public:
	LcdWidgetTable(int id,const QString &title,DprLockManager* lockManager,
			DprLcd *lcd,LcdWidget *parent=0);
	virtual ~LcdWidgetTable();

public:
	virtual void reset();
	virtual void keyEvent(KeyEvent &event);
	virtual void update();
	virtual void show();

	void clear();
	void setItemCount(int count);
	void setItem(int row,LcdWidgetTableItem* item);

protected:
	virtual void draw();

private:
	QList<LcdWidgetTableItem*> m_lstItem;
};

class DprWidgetManager : public QObject
{
	Q_OBJECT
public:
	DprWidgetManager(DprLockManager* lockManager,
			DprLcd *lcd,QObject *parent=0);
	virtual ~DprWidgetManager();

public slots:
	void keyEvent(int code);

protected slots:
	void lcdTimer();

private:
	QMap<int,LcdWidget*> m_widgets;
	QMap<QString,int> m_subWidgetIndex;
	int m_widgetMain;
	int m_widgetMenu;

	int m_currentWidget;

	DprLockManager *m_lockManager;
	DprLcd *m_lcd;

	QTimer *m_timerLcd;
};

#endif /* LCDWIDGET_H_ */
